﻿#include"Bullet.h"
#include"qdebug.h"
Bullet::Bullet(int x, int y, QObject* parent):Articles(x,y)
{
	init();
	m_parent = parent;
}
Bullet::Bullet(QPoint point, QObject* parent):Articles(point.x(), point.y())
{
	init();
	m_parent = parent;
}
void Bullet::init()
{
	m_temp = getCurrentTime();
	//addFrame("子弹射击", "正常");
	setType(Type::weapons);
	/*if (!m_windowSize.isNull())
	{
		setCollider(QRect(QPoint(0, 0), m_windowSize));
	}*/
}

void Bullet::update()
{
	AutoMove();
}

void Bullet::AutoMove()
{
	if (!isActive() /*|| !TimerTrigger()*/)
		return;
	if (m_pause)
	{
		m_temp = getCurrentTime();
		return;
	}
	qreal len = m_Steplength * (getCurrentTime() - m_temp) / 1000.0;
	if (len < 1.0)
		m_SumLen += len;
	if (m_SumLen > 1.0)
	{
		len = m_SumLen;
		m_SumLen = 0;
	}
	m_temp = getCurrentTime();
	QPoint pos(m_vector.x() * len, m_vector.y() * len);
	move(getPos() + pos);
	//qDebug() << m_pos;
	//qDebug() << rectBullet;
	if (!getModelRect().intersects(m_Window))//超出屏幕
	{
		//qDebug() <<"ds"<< getCollider();
		destroy();
	}
	else
	{
		setCollider(getModelRect());
	}
	
}

void Bullet::setProperty(const QVector2D& vector, const int Steplength)
{
	//qDebug() << vector;
	m_vector = vector;
	m_Steplength = Steplength;
}

bool Bullet::Collider(const QRect& collider)
{
	if (Articles::Collider(collider))
	{
		destroy();
		return true;
	}
	return false;
}

bool Bullet::Collider(Articles& collider)
{
	if (Articles::Collider(collider))
	{
		destroy();
		return true;
	}
	return false;
}

const int Bullet::getHarm()
{
	return ATK;
}
